//
//  NewWindowController.m
//  MacDevTest
//
//  Created by 夏利兵 on 2020/8/5.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "NewWindowController.h"
#import "AppDelegate.h"
#import "CustomNSView.h"
#import "LBXOSHud.h"
#import "CustomAlertWindowController.h"
#import "NSCustomAlertView.h"
#import "TagManageView.h"
#import "NSCustomTipView.h"

@interface NewWindowController ()<NSWindowDelegate>
@property (nonatomic, strong) CustomAlertWindowController *noticeWC;
@end

@implementation NewWindowController


- (void)windowWillLoad
{
    [super windowWillLoad];
    NSLog(@"windowWillLoad");
    

}


/* This method can be overridden by advanced users.  It should not be called directly (doing so will cause the window controller to be improperly set up).  Call the -window method to force loading of the nib.
*/
//- (void)loadWindow
//{
//
//}

- (void)windowDidLoad {
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    NSLog(@"windowDidLoad");
    
//    NSMenu *menu = [[NSMenu alloc]initWithTitle:@"menu2"];
//    [NSApp setMainMenu:menu];
 
    
    self.window.title = @"newWindow";
    
    self.window.hasShadow = YES;
    
    [self.window center]; //居中显示
    
    self.window.backgroundColor = [NSColor whiteColor];

//    self.window.contentViewController.view.backgroundFilters = [NSColor blueColor];
    
    
    //样式代码设置
    
//    https://www.jianshu.com/p/d7bedb6637ba
     /**
             只能做是否隐藏之类的操作，其他修改似乎得不到想要的效果
            */
    //        let closeBtn = window.standardWindowButton(.closeButton)
    //        closeBtn.isHidden = true
    //        closeBtn?.image = NSImage.init(named: NSImage.Name.advanced)
            // 设置窗口级别   同级别窗口则后打开的窗口显示在前
    //        window.level = NSWindow.Level(rawValue: NSWindow.Level.RawValue(CGWindowLevelForKey(.maximumWindow)))
            // 窗口是否可以通过点击背景移动
//            window!.isMovableByWindowBackground = true
    
    self.window.movableByWindowBackground = NO;
    
            // 窗口全屏/退出全屏
    //        window.toggleFullScreen(window)
            // 状态了是否透明
    //        window!.titlebarAppearsTransparent = true
            // 设置角标 只有窗口在最小化时才会显示
    //        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
    //            window.dockTile.badgeLabel = "666"
    //        }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.window.dockTile.badgeLabel = @"666";
    });
            
    //        borderless            // 没有顶部titilebar边框
    //        titled                // 有顶部titilebar边框
    //        closable              // 带有关闭按钮
    //        miniaturizable        // 带有最小化按钮
    //        resizable             // 恢复按钮
    //        texturedBackground    // 带纹理背景的window
    //        unifiedTitleAndToolbar// 标题栏和toolBar 下有统一的分割线
    //        fullScreen            // 全屏显示
    //        fullSizeContentView   // contentView会充满整个窗口
            
            /* 下面样式只适用于NSPanel及其子类 */
    //        utilityWindow
    //        docModalWindow
    //        nonactivatingPanel
    //        hudWindow //用于头部显示的panel
            
//            window?.styleMask = [.titled, .hudWindow]   // 窗口样式
    
//    self.window.styleMask
 
    //        borderless            // 没有顶部titilebar边框
    //        titled                // 有顶部titilebar边框
    //        closable              // 带有关闭按钮
    //        miniaturizable        // 带有最小化按钮
    //        resizable             // 恢复按钮
    //        texturedBackground    // 带纹理背景的window
    //        unifiedTitleAndToolbar// 标题栏和toolBar 下有统一的分割线
    //        fullScreen            // 全屏显示
    //        fullSizeContentView   // contentView会充满整个窗口
            
            /* 下面样式只适用于NSPanel及其子类 */
    //        utilityWindow
    //        docModalWindow
    //        nonactivatingPanel
    //        hudWindow //用于头部显示的panel
//    self.window.styleMask = NSWindowStyleMaskBorderless | NSWindowStyleMaskTitled;
    
        
    //左上角关闭，最小化按钮
    
    //隐藏最大化按钮
//     [[self.window standardWindowButton:NSWindowZoomButton] setHidden:YES];
    
    //显示，但是灰色，按不了
    [[self.window standardWindowButton:NSWindowZoomButton] setEnabled:NO];


    //自定义关闭按钮
//    NSButton *closebtn = [self.window standardWindowButton:(NSWindowCloseButton)];
//
//    [closebtn setTarget:self];
//    [closebtn setAction:@selector(closeAction)];

    
    //自定义最小化
//    NSButton*  miniBtn = [self.window standardWindowButton:(NSWindowMiniaturizeButton)];
//
//    [miniBtn setTarget:self];
//
//    [miniBtn setAction:@selector(minAction:)];
    
    
    self.window.delegate = self;
    [self addNotification];
   
}

- (void)minAction:(id)arg
{
    NSLog(@"最小化按钮");
    
//    [[LBXOSHud sharedManager]showHud:@"测试hud测试吹风机打开了撒酒疯大神"];
    
//    [LBXOSHud showScreenToastWithMessage:@"测试hud测试吹风机打开了撒酒疯大神" style:LBXOSHudStyle_Dark font:LBXOSHudFont_Medium delay:2];
//    
//    [LBXOSHud showKeyWindowToastWithMessage:@"fdjaklsjfd;sa中午测试等一下试试" style:LBXOSHudStyle_Dark font:LBXOSHudFont_Small delay:3];
    
    [LBXOSHud showWaitHudWithWindow:self.window message:nil postion:LBXOSHudPostion_Center style:LBXOSHudStyle_Dark];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
        
        [LBXOSHud hideAllWaitingHud];
        
    });
    
}

- (void)closeAction
{
    [self back:nil];
}

- (IBAction)back:(id)sender {
    
    
//    [self testAlertView];
    
    [self testTagView];

    return;
    
    
    AppDelegate *delegate =(AppDelegate*) [NSApplication sharedApplication].delegate;
    
    [delegate.window makeKeyAndOrderFront:nil];
    
    
    //自己隐藏,下面2行代码之一均可
//    [self.window orderOut:nil];
    [self.window close];
}

- (void)testTagView {
    
    [TagManageView showWithSureAction:^{
        
    }];
}
- (IBAction)test1:(id)sender {
    
//    [NSCustomAlertView showWithTitle:@"内容已被修改，是否需要保存?" content:@"邮件将会被保存到草稿箱" type:2 cancel:^{
//
//    } sure:^{
//
//    }];
    
    
    [NSCustomAlertView showWithTitle:@"内容已被修改，是否需要保存?" content:@"邮件将会被保存到草稿箱" type:2 buttons:@[@"取消",@"确认"] defaultIdx:1 action:^(NSInteger idx, NSString *title) {
       
        NSLog(@"idx=%d,title=%@",idx,title);
    }];
    
}
- (IBAction)test2:(id)sender {
    
    [NSCustomAlertView showWithTitle:@"您的邮件主题为空!" content:@"确认要继续发送这封邮件吗?确认要继续发送这封邮件吗?确认要继续发送这封邮件吗?确认要继续发送这封邮件吗?发的发撒的发多少丰富333333333333333" type:2 cancel:^{

        NSLog(@"cancel");
    } sure:^{
        NSLog(@"sure");

    }];
}
- (IBAction)test3:(id)sender {
    
//    [NSCustomAlertView showWithTitle:@"发件人希望得到您签收邮件的回执，是否发送?" content:@"" type:1 cancel:^{
//
//    } sure:^{
//
//    }];
    
    
    [NSCustomAlertView showWithTitle:@"只是显示信息提示alert" content:@"" type:3 cancel:nil sure:nil];
}
- (IBAction)test4:(id)sender {
    
    [self testTagView];
}

- (void)testAlertView {
    
    //    NSCustomAlertView *alertView = [[NSCustomAlertView alloc]init];
    //
    //    NSWindow *keywindow = [NSApplication sharedApplication].keyWindow;
    //
    //    alertView.frame = keywindow.contentView.frame;
    //
    //
    //    alertView.wantsLayer = YES;
    //    alertView.layer.backgroundColor = [NSColor colorWithDeviceWhite:0.8 alpha:0.5].CGColor;
    //
    //    [keywindow.contentView.superview addSubview:alertView];

        
    //    [keywindow.contentViewController.view addSubview:alertView];

        
    //    NSAlertView *alertView = [[NSAlertView alloc]init];
    //
    //    NSWindowController *keyWC = [NSApplication sharedApplication].keyWindow.windowController;
    //
    //    if (!keyWC) {
    //        return;
    //    }
    //
    //    alertView.frame = keyWC.contentViewController.view.frame;
    //
    //    alertView.wantsLayer = YES;
    //    alertView.layer.backgroundColor = [NSColor colorWithDeviceWhite:0.8 alpha:0.5].CGColor;
    //
    //    [keyWC.contentViewController.view addSubview:alertView];
        
  
    //    if  ([NSApplication sharedApplication].keyWindow == self.window ) {
    //
    //        NSLog(@"%@", NSStringFromRect(self.window.frame));
    //    }
    //
    //    //判空，避免重复创建
    //    CustomAlertWindowController *noticeWC  = [[CustomAlertWindowController alloc]initWithWindowNibName:@"CustomAlertWindowController"];
    //
    //
    //    [noticeWC showWindow:nil];
    ////    self.noticeWC = noticeWC;
    //    [[NSApplication sharedApplication] runModalForWindow:noticeWC.window];
    //
}

- (void)dealloc
{
    NSLog(@"NewWindowController dealloc");
}

#pragma mark- window delegate
- (BOOL)windowShouldClose:(NSWindow *)sender
{
    //自定义关闭按钮事件后，收不到该方法响应
    NSLog(@"windowShouldClose");
    
    return YES;
}

//- (nullable id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(nullable id)client;

- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
    NSLog(@"windowWillResize");
    return frameSize;
}

- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame
{
    return newFrame;
}

- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
{
    return YES;
}


#pragma mark- Notification
- (void)addNotification
{
//    https://blog.csdn.net/panna_linda/article/details/78642884
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(windowmove:) name:@"NSWindowDidMoveNotification" object:nil];
}

- (void)windowmove:(NSNotification*)notification
{
    NSLog(@"windowmove:%@",notification.name);
}

- (IBAction)tipAciton1:(id)sender {
    
    
    NSString *str1 = @"2022/08/03";
    
    NSString *str2 = @"2022/10/02";
    
    NSComparisonResult ret = [str1 compare:str2 options:NSNumericSearch];
    
    [NSCustomTipView showWithTipType:0 pos:0 content:@"成功的报文 "];
}
- (IBAction)tipAction2:(id)sender {
    
    
    [NSCustomTipView showWithTipType:1 pos:1 posDiff:80 content:@"fdajl;jfkds中文内容" afterSeconds:5];

}

- (IBAction)tipAction3:(id)sender {
    
    //进度
    NSCustomTipView* tipView =  [NSCustomTipView showWithTipType:NSCustomTipType_CustomProgress pos:2 content:@"成功的报文fffffffffffffffff显示 "];
     
    [self nextDelay:tipView];
    
}

- (void)nextDelay:(NSCustomTipView*)tipView {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        [self afterProgress:tipView];
        
    });

}

- (void)afterProgress:(NSCustomTipView*)tipView {
    
    double progress = [tipView getCustomProgress];
    progress = progress + 0.1;
    
    if (progress >= 1) {
        return;
    }
    
    [tipView setCustomProgress:progress];
    
    [self nextDelay:tipView];
    
}

@end
